<?php 
if (!defined('ROOT')) {
		define('ROOT', dirname(dirname(dirname(__FILE__))));
}
if (!defined('DS')) {
		define('DS', DIRECTORY_SEPARATOR);
	}
$app = ROOT . DS . 'app'. DS;
$js = $app.  'webroot'. DS . 'js'. DS ;
$css = $app.  'webroot'. DS . 'css'. DS ;
$img = $app.  'webroot'. DS . 'img'. DS ;
$files = $app.  'webroot'. DS . 'files'. DS ;
$tmp = $app.  'tmp'. DS;
$media = $app. 'media'. DS;

$_writable_folders = array(   
			$tmp,         
            $media,
            $css,
            $js,
            $img,
			$files,
			ROOT . DS . 'core' . DS . 'vendors' . DS . 'securimage'
		);
        $out = '';
        foreach($_writable_folders as $folder) {
            if (_is_writable_recursive($folder)) {
                $out.= '<tr><td style="background:#00FF00;">Writable</td><td> ' . $folder . '</td></tr>';
            } else {
                $out.= '<tr><td  style="background:#FF0000;">Not Writable</td><td>' . $folder . '</td></tr>';
            }
        }
		$_writable_files = array(
			$app . 'vendors'. DS . 'shells'. DS . 'cron.sh',
			$app . 'vendors'. DS . 'shells'. DS . 'cron.php', 
			ROOT . DS . 'core' . DS . 'cake' . DS . 'console' . DS . 'cake'			
		);
        foreach($_writable_files as $file) {
            if (is_writable($file)) {
                $out.= '<tr><td  style="background:#00FF00;">Writable</td><td> ' . $file . '</td></tr>';
            } else {
				$out.= '<tr><td style="background:#FF0000;">Not Writable</td><td>' . $file . '</td></tr>';
            }
        }
		$tmpCacheFileSize = bytes_to_higher(dskspace($tmp . 'cache'));
        $tmpLogsFileSize = bytes_to_higher(dskspace($tmp . 'logs'));
		$writable = $out;
?>

<?php 
function _is_writable_recursive($dir)
    {
        if (!($folder = @opendir($dir))) {
            return false;
        }
        while ($file = readdir($folder)) {
            if ($file != '.' && $file != '..' && $file != '.svn' && (!is_writable($dir . DS . $file) || (is_dir($dir . DS . $file) && !_is_writable_recursive($dir . DS . $file)))) {
                closedir($folder);
                return false;
            }
        }
        closedir($folder);
        return true;
    }
	function _dskspace($dir) {
        $s = stat($dir);
        $space = $s['blocks']*512;
        if ($space < 0) {//Windows?
            $space = filesize($dir);
        }
        if (is_dir($dir)) {
            $dh = opendir($dir);
            while (($file = readdir($dh)) !== false) {
                if ($file != '.' and $file != '..') {
                    $space+= _dskspace($dir . '/' . $file);
                }
            }
            closedir($dh);
        }
        return $space;
    }
    function dskspace($dir)
    {
        // http://www.php.net/manual/en/function.disk-total-space.php#72324
        if ($output = exec('du -sk ' . $dir)) {
            preg_match('/\d+/', $output, $size_in_kb);
            return $size_in_kb[0] * 1024;
        } else {
            return _dskspace($dir);
        }
    }
    // Bytes to highest unit conversion
    function bytes_to_higher($bytes)
	{
		$symbols = array(
			'B',
			'KB',
			'MB',
			'GB',
			'TB',
			'PB',
			'EB',
			'ZB',
			'YB'
		);
		$exp = floor(log($bytes) /log(1024));
		return sprintf('%.2f ' . $symbols[$exp], ($bytes ? ($bytes/pow(1024, floor($exp))) : 0));
	}
?>


<!-- ------------------------------------------------------------------ html content -------------------------------------------------------------------------------------------------------->

<h2><?php echo 'Server Settings'; ?></h2>
<div>
    <h2><?php echo 'Write Permission Check';?></h2>
    <table border="2">
    <?php echo $writable; ?>
    </table>
</div>

<h2> Server Requirements </h2>
<table border="2">
	
  <tr>
    <td colspan="2"> Settings </td>
    <td> Required Server Settings </td>
    <td> Current  Server Settings </td>
  </tr>
<?php $php_version = PHP_VERSION;
		  $php_version = explode('.', $php_version);
			$class = 'style="background:#FF0000;"';
			if(5 == $php_version[0] && 2 == $php_version[1]){
				$class = 'style="background:#00FF00;"';
			}	
?>  
  <tr  <?php echo $class; ?>>
    <td colspan="2"> PHP Version </td>
    <td> 5.x (preferably 5.2+ and < 5.3.0) </td>
    <td> <?php echo PHP_VERSION; ?>	</td>
  </tr>
<?php 
		$class = 'style="background:#FF0000;"';
		if(function_exists(mysql_get_client_info)){
			$sql_version =  mysql_get_client_info();
			$php_version = explode('.', $sql_version);
			if(5 <= $php_version[0]){
				$class = 'style="background:#00FF00;"';
			}	
			else{
				$requird .= "<li> MySQL Version Should be 5.x </li>";
			}		
		}
		else{
			$sql_version = "-";
		}
?> 
  <tr <?php echo $class; ?>>
    <td colspan="2"> MySQL Version </td>
    <td> 5.x </td>
    <td> <?php 	echo $sql_version;  ?>		</td>
  </tr>
<?php 
	if(function_exists(get_extension_funcs)){
		if(get_extension_funcs('gd')){
			$gd_info = gd_info();
			$gd_version = explode(' ', $gd_info['GD Version']);
			$gd_version = str_replace("(", "", $gd_version[1]);
			$gd_version = explode('.', $gd_version);
			if($gd_version[0] >= 2){
				$class = 'style="background:#00FF00;"';							
			}
			else{
				$requird .= "<li> PHP Extension GD Version should be need  2.x </li>";
			}	
			$gd_version = $gd_info['GD Version'];
		}
		else{
			$gd_version = " Not Installed";
		}
	}
?> 
  <tr <?php echo $class; ?>>
    <td rowspan="4"> Extensions </td>
    <td> GD </td>
    <td> GD Version - 2.x+ </td>
    <td> <?php echo $gd_version; ?> </td>
  </tr>
<?php 
	  $class = 'style="background:#FF0000;"';
	  if(function_exists(get_extension_funcs)){
			if(get_extension_funcs('pcre')){
				$pcre_version = PCRE_VERSION;
				$pcre_versions = explode('.', $pcre_version);				
				if(7 <= $pcre_versions[0]){
					$class = 'style="background:#00FF00;"';
				}
				else{
					$requird .= "<li> PHP Extension PCRE Version should be need  7.x </li>";
				}
			}
			else{
				$pcre_version = "Not Installed";
			}
		}
?>    
  <tr <?php echo $class; ?>>
    <td> PCRE </td>
    <td>  PCRE Version - 7.x+ </td>
    <td><?php 
				echo $pcre_version;
		 ?></td>
  </tr>
<?php 
	  $class = 'style="background:#FF0000;"';
	  if(function_exists(get_extension_funcs)){
			if(get_extension_funcs('curl')){
				$curl_info = curl_version();
				$curl_infos = explode('.', $curl_info['version']);
				if(7 <= $curl_infos[0]){
					$curl_info = $curl_info['version'];
					$class = 'style="background:#00FF00;"';
				}
				else{
					$requird .= "<li> PHP Extension CURL Version should be need  7.x </li>";
				}
			}
			else{
				$requird .= "<li> PHP Extension CURL Version should be need  7.x </li>";
				$curl_info = "Not Installed";
			}
		}
?>  
  <tr <?php echo $class; ?>>
    <td> CURL </td>
    <td> CURL version - 7.x+ </td>
    <td><?php echo $curl_info; ?></td>
  </tr>
<?php 
		$class = 'style="background:#FF0000;"';
		if(function_exists(get_extension_funcs)){
			if(get_extension_funcs('json')){
				$class = 'style="background:#FF6600;"';
				$json_info = "Installed [don't know version]";
			}
			else{
				$json_info = "Not Installed";
				$requird .= "<li> PHP Extension JSON Version should be need  1.x </li>";
			}
		}
?> 
  <tr  <?php echo $class; ?>>
    <td> JSON </td>
    <td> json version - 1.x+ </td>
    <td> <?php echo $json_info; ?>   </td>
  </tr>
<?php 
		$class = 'style="background:#FFFF00;"';
		if(function_exists(ini_get)){					
				$max_execution_time = ini_get('max_execution_time');
				if($max_execution_time >= 180)
					$class = 'style="background:#00FF00;"';				
		}
		else{
			$max_execution_time = '[don\'t know max_execution_time]';
		}
 ?>  
  <tr <?php echo $class; ?>>
    <td rowspan="5"> php.ini settings </td>
    <td> max_execution_time </td>
    <td> 180 (not mandatory) </td>
    <td>
    	<?php echo $max_execution_time; ?>
    </td>
  </tr>
<?php 
		$class = 'style="background:#FFFF00;"';
		if(function_exists(ini_get)){					
				$max_input_time = ini_get('max_input_time');
				if($max_input_time >= 6000)
					$class = 'style="background:#00FF00;"';
		}
		else{
			$max_input_time = '[don\'t know max_input_time]';
		}
 ?>  
  <tr <?php echo $class; ?>>
    <td> max_input_time </td>
    <td> 6000 (not mandatory) </td>
    <td>
    	<?php echo $max_input_time; ?>
    </td>
  </tr>
<?php 
		$class = 'style="background:#FF0000;"';
		if(function_exists(ini_get)){					
				$memory_limit = ini_get('memory_limit');
				$memory_limits = str_replace("M", "", $memory_limit);
				if($memory_limits >= 32 && $memory_limits < 128)
						$class = 'style="background:#FF6600;"';
				else if($memory_limits >= 128)
						$class = 'style="background:#00FF00;"';
				else{
					$requird .= "<li> php.ini settings Memory Limit should be minimum 32M</li>";
				}
		}
		else{
			$memory_limit = '[don\'t know memory_limit]';
		}
 ?>  
  <tr <?php echo $class; ?>>
    <td> memory_limit </td>
    <td> 128M (at least 32M) </td>
    <td>
    	<?php echo $memory_limit; ?>
    </td>
  </tr>
<?php 
		$class = 'style="background:#FF0000;"';
		if(function_exists(ini_get)){					
				if(ini_get('safe_mode'))
				{					
					$safe_mode = "ON";
				}
				else{
					$class = 'style="background:#00FF00;"';
					$safe_mode = "OFF";
				}
		}
		else{
			$class = 'style="background:#FF6600;"';
			$safe_mode = '[don\'t know safe_mode status]';
		}
?>  
  <tr <?php echo $class; ?>>
    <td> safe_mode </td>
    <td> OFF </td>
    <td>
    	<?php echo $safe_mode; ?>
    </td>
  </tr>
<?php 
		$class = 'style="background:#FF0000;"';
		if(function_exists(ini_get)){					
				if(ini_get('open_basedir'))
				{
					$open_basedir = ini_get('open_basedir');
				}
				else{	
					$class = 'style="background:#00FF00;"';
					$open_basedir = "No Value";
				}
		}
		else{
			$class = 'style="background:#FF6600;"';
			$open_basedir = '[don\'t know open_basedir status]';
		}
 ?>  
  <tr <?php echo $class; ?>>
    <td> open_basedir </td>
    <td> No Value </td>
     <td>
    	<?php echo $open_basedir; ?>
    </td>
  </tr>
<?php 
	$class = 'style="background:#FF0000;"';
	if(function_exists(apache_get_version)){	
		$apace_version = apache_get_version();
		$apace_version_info = explode(" ", $apace_version);
		$apace_version_info = explode('/', $apace_version_info[0]);
		$apace_version_info = explode('.', $apace_version_info[1]);
		if($apace_version_info[0] >= 2)
			$class = 'style="background:#00FF00;"';
		else if($apace_version_info[0] == 1)	
			$class = 'style="background:#FF6600;"';
	}	
	else{	
		$version = explode(" ",$_SERVER["SERVER_SOFTWARE"],3);
		$apace_version_info = explode('/', $version[0]);
		$apace_version_info = explode('.', $apace_version_info[1]);
		if($apace_version_info[0] >= 2)
			$class = 'style="background:#00FF00;"';
		else if($apace_version_info[0] == 1)	
			$class = 'style="background:#FF6600;"';
		$apace_version = $version[0];
	}	
?>
  <tr <?php echo $class; ?>>
    <td colspan="2">Apache  </td>
    <td> 1+ (preferably 2+) </td>
    <td>
    	<?php echo $apace_version; ?>
    </td>
  </tr>
<?php 
	if(function_exists(apache_get_modules)){
		$modules = apache_get_modules();
		$class = 'style="background:#FF0000;"';
		if (in_array("mod_rewrite", $modules)) {
			   $class = 'style="background:#00FF00;"';
			   $mod_rewrite = "Loaded";
		}	
		else{
			   $mod_rewrite = "Not Loaded";
		}
?>  
 <tr <?php echo $class; ?>>
    <td rowspan="4"> Loaded  Modules </td>
    <td> mod_rewrite </td>
    <td> load </td>
    <td> <?php echo $mod_rewrite; ?> </td>
  </tr>
<?php 
		$class = 'style="background:#FFFF00;"';
		if (in_array("mod_deflate", $modules)) {
			$class = 'style="background:#00FF00;"';
			$mod_deflate = "Loaded";
		}
		else{
			$mod_deflate = "Not Loaded";
		}	
?>  
  <tr <?php echo $class; ?>>
    <td> mod_deflate (not mandatory, but highly recommended for better performance - gzip) </td>
    <td> load </td>
    <td><?php  echo $mod_deflate; ?></td>
  </tr>
<?php 
		$class = 'style="background:#FFFF00;"';
		if (in_array("mod_rewrite", $modules)) {
			   $class = 'style="background:#00FF00;"';
			   $mod_rewrite = "Loaded";
		}	
		else{
			   $mod_rewrite = "Not Loaded";
		}	
 ?>  
  <tr  <?php echo $class; ?>>
    <td> mod_expires (not mandatory, but highly recommended for better performance - browser caching)</td>
    <td> load </td>
    <td><?php  echo $mod_rewrite; ?></td>
  </tr>
<?php
	}
?>    
</table>

<?php if(!empty($requird)){ ?>

<h2><?php echo 'Must Wanted Server Settings'; ?></h2>
<ul style="background:#FF0000;">
	<?php echo $requird; ?>
</ul>

<?php } ?>


 <!-------------------------------------------------------------------- html content -------------------------------------------------------------------------------------------------------->
